package org.lds.simple.disruptor.producer.sequencer;

import org.lds.simple.disruptor.SequenceBarrier;
import org.lds.simple.disruptor.producer.cache.RightCacheValuePadding;

import java.util.concurrent.locks.LockSupport;

public class SingleProducerSequencer extends RightCacheValuePadding {
    @Override
    public long next() {
        SequenceBarrier sequenceBarrier = getSequenceBarrier();

        long nextProducerSequence;
        while ((nextProducerSequence = allowProduce(this.producerSequenceCache, this.minConsumerSequenceCache)) == -1L) {
            LockSupport.parkNanos(1L);

            this.minConsumerSequenceCache = sequenceBarrier.getCurrMinConsumerSequence();
        }

        this.producerSequenceCache = nextProducerSequence;
        return nextProducerSequence;
    }

}
